package com.example.clxy.service.impl;

import com.example.clxy.dao.UniversityMapper;
import com.example.clxy.entity.University;
import com.example.clxy.result.PageResult;
import com.example.clxy.result.WebRestResult;
import com.example.clxy.result.business.PageResult.UniversityPageResult;
import com.example.clxy.service.UniversityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Hsieh
 * @title: UniversityServiceImpl
 * @projectName clxy
 * @description:
 * @date 2020/8/1021:40
 */
@Service
public class UniversityServiceImpl implements UniversityService {
    @Autowired
    UniversityMapper universityMapper;
    @Override
    public WebRestResult addUniversity(University university) {
        WebRestResult result = new WebRestResult();
        university.setCreatedTime(new Date());
        university.setUpdatedTime(new Date());
        if (universityMapper.insert(university)==1){
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("添加成功！");
        }else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("添加失败！");
        }
        return result;
    }

    @Override
    public WebRestResult deleteUniversity(String universityid) {
        WebRestResult result=new WebRestResult();
        if (universityMapper.deleteByid(universityid)==1){
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("删除成功！");
        }else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除失败！");
        }
        return  result;
    }

    @Override
    public WebRestResult updateUniversity(University university) {
        WebRestResult result=new WebRestResult();
        university.setUpdatedTime(new Date());
        if (universityMapper.updateByPrimaryKey(university)==1){
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("修改成功！");
        }else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("修改失败！");
        }
        return result;
    }

    @Override
    public PageResult<UniversityPageResult> getUniversitybyname(String universityname , int page, int pagesize) {
        PageResult<UniversityPageResult> result = new PageResult<>();
        int begin = (page - 1) * pagesize;
        int end = pagesize;
        List<UniversityPageResult> list =universityMapper.getUniversitybyname (universityname, begin, end, pagesize);
        if (list.size() > 0) {
            result.setTotal(list.get(0).getCnt());
            result.setPageCount(list.get(0).getPage());
            result.setRows(list);
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("获取列表成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("获取列表失败！");
        }
        return result;
    }

    @Override
    public PageResult<UniversityPageResult> getUniversitybyid(String universityid, int page, int pagesize) {
        PageResult<UniversityPageResult> result = new PageResult<>();
        int begin = (page - 1) * pagesize;
        int end = pagesize;
        List<UniversityPageResult> list =universityMapper.getUniversitybyid (universityid, begin, end, pagesize);
        if (list.size() > 0) {
            result.setTotal(list.get(0).getCnt());
            result.setPageCount(list.get(0).getPage());
            result.setRows(list);
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("获取列表成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("获取列表失败！");
        }
        return result;
    }
}
